// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Provides a RedirectHandler.PageConfig object for logging in and out.
 */

goog.provide('rb.auth.pageConfigs.login');
goog.provide('rb.auth.pageConfigs.logout');

goog.require('relief.handlers.RedirectHandler');


/**
 * Returns a URL to the server that will redirect the user to the referer after
 * logging in.
 *
 * @param {relief.nav.Path} path The current path.
 * @param {relief.nav.Path} referer The previous path.
 * @return {string} The path to redirect to.
 *
 * @private
 */
rb.auth.pageConfigs.getLoginRedirect_ = function(path, referer) {
  var refPath = referer.hash.replace(/^\//, '');
  return '/api/auth/login/' + refPath;
};


/**
 * Returns a URL to the server that will redirect the user to the referer after
 * logging out.
 *
 * @param {relief.nav.Path} path The current path.
 * @param {relief.nav.Path} referer The previous path.
 * @return {string} The path to redirect to.
 *
 * @private
 */
rb.auth.pageConfigs.getLogoutRedirect_ = function(path, referer) {
  var refPath = referer.hash.replace(/^\//, '');
  return '/api/auth/logout/' + refPath;
};


/**
 * Log-in page config.
 *
 * @type {relief.handlers.RedirectHandler.PageConfig}
 */
rb.auth.pageConfigs.login = {
  handler: relief.handlers.RedirectHandler,
  fromReferer: rb.auth.pageConfigs.getLoginRedirect_,
  scope: relief.handlers.RedirectHandler.Scope.PAGE,
  url: undefined
};


/**
 * Log-out page config.
 *
 * @type {relief.handlers.RedirectHandler.PageConfig}
 */
rb.auth.pageConfigs.logout = {
  handler: relief.handlers.RedirectHandler,
  fromReferer: rb.auth.pageConfigs.getLogoutRedirect_,
  scope: relief.handlers.RedirectHandler.Scope.PAGE,
  url: undefined
};
